
const http = require("http")
const url = require("url")

const ModuleRenderStatus = require("./module/renderStatus.js")
const ModuleRenderHTML = require("./module/renderHtml.js")
// 创建服务器的专门核心代码
/*http.createServer((req, res)=> {
  // req 接收浏览器传的参数
 if (req.url === "/favicon.ico") {
   return
 }
  console.log(req.url)
  // res 返回渲染内容
  res.writeHead(ModuleRenderStatus.renderStatus(req.url), {"Content-Type": "text/html;charset=utf-8"})
  res.write(ModuleRenderHTML.renderHTML(req.url)) // 往浏览器输出东西
  res.end() // 只能传字符串

}).listen(3000, ()=> {
  console.log("server start")
})*/

// 与下面这种方法一样
const server = http.createServer()
server.on("request", (req,res)=> {
  if (req.url === "/favicon.ico") {
    return
  }
  const urlObj = url.parse(req.url, true)
  console.log(urlObj.query)
  const pathname = url.parse(req.url).pathname
  // res 返回渲染内容
  res.writeHead(ModuleRenderStatus.renderStatus(pathname), {"Content-Type": "text/html;charset=utf-8"})
  res.write(ModuleRenderHTML.renderHTML(pathname)) // 往浏览器输出东西
  res.end() // 只能传字符串
})

server.listen(3000,()=> {
  console.log("server start")
})

const urlString = 'https://www.baidu.com:443/ad/index.html?id=8&name=mouse#tag=110'
const parsedStr = url.parse(urlString)
console.log(parsedStr)

// const url111 =  {
//   protocol: 'https:',
//     slashes: true,
//     auth: null,
//     host: 'www.baidu.com:443',
//     port: '443',
//     hostname: 'www.baidu.com',
//     hash: '#tag=110',
//     search: '?id=8&name=mouse',
//     query: 'id=8&name=mouse',
//     pathname: '/ad/index.html',
//     path: '/ad/index.html?id=8&name=mouse',
//     href: 'https://www.baidu.com:443/ad/index.html?id=8&name=mouse#tag=110'
// }
//
// console.log(url.format(url111))

const a = url.resolve('/one/two/three', 'four') // 注意最后加/,和不加/的区别
const b = url.resolve('http://example.com/one/two/three/', '/four') // 注意最后加/,和不加/的区别

/** 加/ 是拼接，不加/是替换, 后面的加/就是替换*/
console.log(a)
console.log(b)
